package rc.work.module.infra.controller.admin.onlyoffice.filetable;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import rc.work.framework.apilog.core.annotation.ApiAccessLog;
import rc.work.framework.common.pojo.CommonResult;
import rc.work.framework.common.pojo.PageParam;
import rc.work.framework.common.pojo.PageResult;
import rc.work.framework.common.util.object.BeanUtils;
import rc.work.framework.excel.core.util.ExcelUtils;

import rc.work.module.infra.controller.admin.onlyoffice.filetable.vo.FileTablePageReqVO;
import rc.work.module.infra.controller.admin.onlyoffice.filetable.vo.FileTableRespVO;
import rc.work.module.infra.controller.admin.onlyoffice.filetable.vo.FileTableSaveReqVO;
import rc.work.module.infra.dal.dataobject.file.FileDO;
import rc.work.module.infra.service.onlyoffice.filetable.FileTableService;

import java.io.IOException;
import java.util.List;

import static rc.work.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static rc.work.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 文件绑定表信息")
@RestController
@RequestMapping("/infra/file-table")
@Validated
public class FileTableController {

    @Resource
    private FileTableService fileTableService;

    @PostMapping("/create")
    @Operation(summary = "创建文件绑定表信息")
//    @PreAuthorize("@ss.hasPermission('dev:file-table:create')")
    public CommonResult<Long> createFileTable(@Valid @RequestBody FileTableSaveReqVO createReqVO) {
        return success(fileTableService.createFileTable(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件绑定表信息")
//    @PreAuthorize("@ss.hasPermission('dev:file-table:update')")
    public CommonResult<Boolean> updateFileTable(@Valid @RequestBody FileTableSaveReqVO updateReqVO) {
        fileTableService.updateFileTable(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件绑定表信息")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('dev:file-table:delete')")
    public CommonResult<Boolean> deleteFileTable(@RequestParam("id") Long id) {
        fileTableService.deleteFileTable(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件绑定表信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('dev:file-table:query')")
    public CommonResult<FileTableRespVO> getFileTable(@RequestParam("id") Long id) {
        FileDO fileTable = fileTableService.getFileTable(id);
        return success(BeanUtils.toBean(fileTable, FileTableRespVO.class));
    }

    @GetMapping("/downFile")
    @Operation(summary = "获得文件绑定表信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('dev:file-table:query')")
    public CommonResult<FileTableRespVO> downFile(@RequestParam("id") Long id) {
        FileDO fileTable = fileTableService.downFile(id);
        return success(BeanUtils.toBean(fileTable, FileTableRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件绑定表信息分页")
//    @PreAuthorize("@ss.hasPermission('dev:file-table:query')")
    public CommonResult<PageResult<FileTableRespVO>> getFileTablePage(@Valid FileTablePageReqVO pageReqVO) {
        PageResult<FileDO> pageResult = fileTableService.getFileTablePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FileTableRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件绑定表信息 Excel")
//    @PreAuthorize("@ss.hasPermission('dev:file-table:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFileTableExcel(@Valid FileTablePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FileDO> list = fileTableService.getFileTablePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件绑定表信息.xls", "数据", FileTableRespVO.class,
                        BeanUtils.toBean(list, FileTableRespVO.class));
    }

}